﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using hnrzxt.Models;
using hnrzxt.data;
using Model;

namespace hnrzxt.Controllers
{
    public class Menu : Controller
    {
        private readonly ApplicationDbContext _db;

        public Menu(ApplicationDbContext db)
        {
            _db = db;
        }

        public List<MenuManage> GetMenuTreeData()
        {
            List<MenuManage> menulist = _db.MenuManage.ToList();
            return menulist;

        }
        public List<MenuManage> GetMenuByRid(int rid)
        {
            var data = from p in _db.MenuManage
                       select p;

            data = data.Where(p => _db.Roles_Menu.Where(pt => pt.Roleid == rid).Select(pt => pt.Menuid).Contains(p.Id));

            List<MenuManage> mm = data.ToList();

            return mm;
        }


        //GET: Menu
        public async Task<IActionResult> Index()
        {
              return _db.MenuManage != null ? 
                          View(await _db.MenuManage.ToListAsync()) :
                          Problem("Entity set 'ApplicationDbdb.MenuManage'  is null.");
        }

        // GET: Menu/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null || _db.MenuManage == null)
            {
                return NotFound();
            }

            var menuManage = await _db.MenuManage
                .FirstOrDefaultAsync(m => m.Id == id);
            if (menuManage == null)
            {
                return NotFound();
            }

            return View(menuManage);
        }

        // GET: Menu/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: Menu/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,ParentId,MenuURL,Icon,Type")] MenuManage menuManage)
        {
            if (ModelState.IsValid)
            {
                _db.Add(menuManage);
                await _db.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(menuManage);
        }

        // GET: Menu/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null || _db.MenuManage == null)
            {
                return NotFound();
            }

            var menuManage = await _db.MenuManage.FindAsync(id);
            if (menuManage == null)
            {
                return NotFound();
            }
            return View(menuManage);
        }

        // POST: Menu/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,Name,ParentId,MenuURL,Icon,Type")] MenuManage menuManage)
        {
            if (id != menuManage.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _db.Update(menuManage);
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!MenuManageExists(menuManage.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(menuManage);
        }

        // GET: Menu/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null || _db.MenuManage == null)
            {
                return NotFound();
            }

            var menuManage = await _db.MenuManage
                .FirstOrDefaultAsync(m => m.Id == id);
            if (menuManage == null)
            {
                return NotFound();
            }

            return View(menuManage);
        }

        // POST: Menu/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            if (_db.MenuManage == null)
            {
                return Problem("Entity set 'ApplicationDbdb.MenuManage'  is null.");
            }
            var menuManage = await _db.MenuManage.FindAsync(id);
            if (menuManage != null)
            {
                _db.MenuManage.Remove(menuManage);
            }
            
            await _db.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool MenuManageExists(int id)
        {
          return (_db.MenuManage?.Any(e => e.Id == id)).GetValueOrDefault();
        }


        public async Task<IActionResult> BasicInfos()
        {
            return View(await _db.hr_jbxx.ToListAsync());

        }
        public List<hr_jbxx> GetheInfosData()
        {
            List<hr_jbxx> infolist = _db.hr_jbxx.ToList();
            return infolist;


       
        }
    }
}
